package com.java.yz.day04.homework;

//数组元素的逆序（把元素对调）

public class ArrayText3 {
    public static void main(String[] args) {
        int[] arr1={1,2,3,4,5};
        System.out.println("逆序之前：");
        ArrayPrint(arr1);
        int[] arr2=new int[5];

        //解决方案1
        for (int i = 0; i < arr1.length; i++) {
            arr2[i]=arr1[arr1.length-i-1];
            //System.out.println("arr2["+i+"]"+"="+arr2[i]);
        }
        System.out.println("逆序之后：");
        ArrayPrint(arr2);

    //解决方案2
    /*
                           [1,2,3,4,5]
         start++     -->              <--    end--
     */
        for (int start = 0,end=arr1.length-1; start < end; start++,end--) {
            int temp=arr1[start];
            arr1[start]=arr1[end];
            arr1[end]=temp;
        }
        ArrayPrint(arr1);
}
    //这是封装的一个遍历的方法
    public static void ArrayPrint(int[] array){
        for (int i = 0; i < array.length; i++) {
            if(i==0){
                System.out.print("["+array[0]+",");
            }
            else if (i==array.length-1){
                System.out.println(array[array.length-1]+"]");
            }
            else {
                System.out.print(array[i]+",");
            }
        }
    }
}
